﻿using System;
using System.Web;
using System.Data;
using System.Text;
using System.Web.Mvc;
using System.Collections.Generic;
using eShop.Core;
using eShop.Services;
using eShop.Web.Framework;
using eShop.Web.Admin.Models;

namespace eShop.Web.Admin.Controllers
{
    /// <summary>
    /// 商品明细
    /// </summary>
    public partial class ProductDetailsController : BaseAdminController
    {

        /// <summary>
        /// 明细商品选择列表
        /// </summary>
        /// <param name="pid">商品id</param>
        /// <param name="pageSize">每页数</param>
        /// <param name="pageNumber">当前页数</param>
        /// <returns></returns>
        public ContentResult SelectList(int pid, int pageNumber = 1, int pageSize = 24)
        {
            string condition = string.Format(" pid = {0}", pid);
            PageModel pageModel = new PageModel(pageSize, pageNumber, AdminProductDetails.AdminGetProductDetailsList(pid).Rows.Count);
            DataTable brandSelectList = AdminProductDetails.AdminGetProductDetailsSelectList(pageModel.PageSize, pageModel.PageNumber, condition);
            StringBuilder result = new StringBuilder("{");
            result.AppendFormat("\"totalPages\":\"{0}\",\"pageNumber\":\"{1}\",\"items\":[", pageModel.TotalPages, pageModel.PageNumber);
            foreach (DataRow row in brandSelectList.Rows)
                result.AppendFormat("{0}\"id\":\"{1}\",\"name\":\"{2}\"{3},", "{", row["sid"], row["vin"].ToString().Trim() , "}");

            if (brandSelectList.Rows.Count > 0)
                result.Remove(result.Length - 1, 1);

            result.Append("]}");
            return Content(result.ToString());
        }



        /// <summary>
        /// 明细列表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public ActionResult List(int pid)
        {
            ProductsdetailsModel model = new ProductsdetailsModel();
            model.DetailList = AdminProductDetails.AdminGetProductDetailsList(pid);
            //第一个
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            ShopUtils.SetAdminRefererCookie(string.Format("{0}?pid={1}", Url.Action("list"), pid));
            ViewData["pid"] = pid;//商品号
            return View(model);
        }


        /// <summary>
        /// 导入页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Import()
        {
            /* ShopUtils.SetAdminRefererCookie(string.Format("{0}?pid={1}", Url.Action("list"), pid))*/
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return View();
        }


        /// <summary>
        /// 添加管理员组
        /// </summary>
        [HttpGet]
        public ActionResult Add(int pid)
        {
            ProductsdetailsModel model = new ProductsdetailsModel();
            model.pid = pid;
            model.oid = 0;
            model.DetailList = AdminStock.AdminGetStockList();
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return View(model);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(ProductsdetailsModel model)
        {
            if (ModelState.IsValid)
            {
                ProductDetailsInfo info = new ProductDetailsInfo()
                {
                    Pid = model.pid,
                    Vin = model.vin,
                    Gde = model.gde,
                    Conf1 = model.conf1,
                    Conf2 = model.conf2,
                    Conf3 = model.conf3,
                    Conf4 = model.conf4,
                    Conf5 = model.conf5,
                    Conf6 = model.conf6,
                    Oid = model.oid == null ? 0 : model.oid,
                    Description = model.description,

                };
                AdminProductDetails.CreateProductDetails(info);
                AddAdminOperateLog("添加明细商品", "添加明细商品:" + model.vin);
                return PromptView("添加成功");
            }
            return View(model);
        }

        /// <summary>
        /// 编辑_明细商品
        /// </summary>
        /// <param name="sid">件号</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Edit(int sid = -1)
        {
            ProductsdetailsModel model = new ProductsdetailsModel();
            ProductDetailsInfo info = AdminProductDetails.GetProductDetailsBysid(sid);
            if (info != null)
            {
                model.sid = info.Sid;
                model.pid = info.Pid;
                model.vin = info.Vin.Trim();
                model.gde = info.Gde.Trim();
                model.conf1 = info.Conf1.Trim();
                model.conf2 = info.Conf2.Trim();
                model.conf3 = info.Conf3.Trim();
                model.conf4 = info.Conf4.Trim();
                model.conf5 = info.Conf5.Trim();
                model.conf6 = info.Conf6.Trim();
                model.oid = info.Oid == null ? 0 : info.Oid;
                model.description = info.Description.Trim();
            }
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return View(model);
        }

        /// <summary>
        /// 编辑_明细商品
        /// </summary>
        [HttpPost]
        public ActionResult Edit(ProductsdetailsModel model, int sid = -1)
        {
            ProductDetailsInfo info = new ProductDetailsInfo();
            info.Sid = model.sid;
            info.Pid = model.pid;
            info.Vin = model.vin;
            info.Gde = model.gde;
            info.Conf1 = model.conf1;
            info.Conf2 = model.conf2;
            info.Conf3 = model.conf3;
            info.Conf4 = model.conf4;
            info.Conf5 = model.conf5;
            info.Conf6 = model.conf6;
            info.Oid = model.oid;
            info.Description = model.description;
            AdminProductDetails.UpdateProductDetails(info);
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return PromptView("修改成功");
        }

        /// <summary>
        /// 删除_明细商品
        /// </summary>
        public ActionResult Del(int sid = -1)
        {
            int result = AdminProductDetails.DeleteProductDetailsId(sid);
            if (result == 1)
            {
                AddAdminOperateLog("删除明细商品", "删除明细商品id=:" + sid);
                return PromptView("删除成功");
            }
            else
            {
                return PromptView("删除失败");
            }
        }

    }
}
